using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;

namespace NinjaToolbox.PhysicsSystem
{
    [CustomEditor(typeof(PhysicsAdsorbPoint))]
    public class PhysicsAdsorbPointInspector : Editor
    {
        Vector3[] mAdsorptionResult;
        Vector3 mScrollView;


        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            var convObj = base.target as PhysicsAdsorbPoint;

            if (GUILayout.Button("Adsorption"))
            {
                mAdsorptionResult = Adsorption(convObj.transform, 20000f);
            }

            if (mAdsorptionResult != null)
            {
                mScrollView = GUILayout.BeginScrollView(mScrollView);
                for (int i = 0; i < mAdsorptionResult.Length; i++)
                {
                    var item = mAdsorptionResult[i];
                    if (GUILayout.Button(i + " " + item.ToString()))
                    {
                        Undo.RecordObject(convObj.transform, "PhysicsAdsorptionPoint");

                        convObj.transform.position = item;
                    }
                }
                GUILayout.EndScrollView();
            }

        }

        Vector3[] Adsorption(Transform transform, float radius = 10f)
        {
            var hits = new List<PhysicsUtil.RaycastHit>();
            hits.AddRange(PhysicsUtil.RaycastAll(new Ray(transform.position, Vector3.up)));
            hits.AddRange(PhysicsUtil.RaycastAll(new Ray(transform.position, Vector3.down)));

            hits.AddRange(PhysicsUtil.RaycastAll(new Ray(transform.position, Vector3.left)));
            hits.AddRange(PhysicsUtil.RaycastAll(new Ray(transform.position, Vector3.Lerp(Vector3.left, Vector3.up, 0.5f))));
            hits.AddRange(PhysicsUtil.RaycastAll(new Ray(transform.position, Vector3.Lerp(Vector3.left, Vector3.down, 0.5f))));

            hits.AddRange(PhysicsUtil.RaycastAll(new Ray(transform.position, Vector3.right)));
            hits.AddRange(PhysicsUtil.RaycastAll(new Ray(transform.position, Vector3.Lerp(Vector3.right, Vector3.up, 0.5f))));
            hits.AddRange(PhysicsUtil.RaycastAll(new Ray(transform.position, Vector3.Lerp(Vector3.right, Vector3.down, 0.5f))));

            hits.AddRange(PhysicsUtil.RaycastAll(new Ray(transform.position, Vector3.forward)));
            hits.AddRange(PhysicsUtil.RaycastAll(new Ray(transform.position, Vector3.Lerp(Vector3.forward, Vector3.up, 0.5f))));
            hits.AddRange(PhysicsUtil.RaycastAll(new Ray(transform.position, Vector3.Lerp(Vector3.forward, Vector3.down, 0.5f))));

            hits.AddRange(PhysicsUtil.RaycastAll(new Ray(transform.position, Vector3.back)));
            hits.AddRange(PhysicsUtil.RaycastAll(new Ray(transform.position, Vector3.Lerp(Vector3.back, Vector3.up, 0.5f))));
            hits.AddRange(PhysicsUtil.RaycastAll(new Ray(transform.position, Vector3.Lerp(Vector3.back, Vector3.down, 0.5f))));

            if (hits.Count > 0)
            {
                hits.Sort((x, y) => Vector3.Distance(x.ContactPoint, transform.position).CompareTo(Vector3.Distance(y.ContactPoint, transform.position)));

                return hits.ConvertAll(m => m.ContactPoint).ToArray();
            }
            else
            {
                return new Vector3[] { transform.position };
            }
        }
    }
}
